/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kiakaku.downloadpage;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;
import net.htmlparser.jericho.TextExtractor;
import org.kiakaku.utility.FileUtility;

/*
 *   File download is created by kiakaku on Mar 15, 2012 9:13:52 PM
 * If you want modify this file please insert information in here 
 * - Last modify in Mar 15, 2012, 9:13:52 PM by kiakaku
 *                  
 *
 * @author kiakaku
 */
public class download {

    List<Node> source = null;
    List<Node> all = null;

    public download() {
        source = FileUtility.docListNode(OutLink.SOURCE_FILE);
        all = FileUtility.docListNode(OutLink.TAGET_FILE);
        for (Node a : source) {
            if (!(all.contains(a))) {
                all.add(a);
            }
        }
    }

    public void downloadAll() {
        for (Node node : all) {
            downloadOneSite(node);
        }
    }

    public void downloadOneSite(Node site) {
        try {
            Source sourceTemp = new Source(new URL(site.getUrl()));
            TextExtractor txtExtract = null;
            txtExtract = new TextExtractor(sourceTemp) {

                @Override
                public boolean excludeElement(StartTag st) {
                    return (st.getName() == null ? HTMLElementName.INPUT == null : st.getName().equals(HTMLElementName.INPUT))
                            || (st.getName() == null ? HTMLElementName.A == null : st.getName().equals(HTMLElementName.A))
                            || "footer box19".equalsIgnoreCase(st.getAttributeValue("class"))
                            || (st.getName() == null ? HTMLElementName.META == null : st.getName().equals(HTMLElementName.META))
                            || "/zone-26/event.htm".equalsIgnoreCase(st.getAttributeValue("href"))
                            || "javascript:;".equalsIgnoreCase(st.getAttributeValue("href"))
                            || "comment".equalsIgnoreCase(st.getAttributeValue("id"))
                            || "commentform".equalsIgnoreCase(st.getAttributeValue("id"))
                            || "respond".equalsIgnoreCase(st.getAttributeValue("id"))
                            || "widget".equalsIgnoreCase(st.getAttributeValue("class"))
                            || "footer".equalsIgnoreCase(st.getAttributeValue("id"));

                }
            };
            FileUtility.ghiFile(site.getUrl() + "\n" + txtExtract.toString(), "data\\" + site.getId() + ".data");
            txtExtract = null;
        } catch (IOException ex) {
            Logger.getLogger(OutLink.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
